// ///////////////////////////// MIT License //////////////////////////////////// //
//                                                                                //
// Copyright (c) 2010 David Zsolt Manrique                                        //
//                    david.zsolt.manrique@gmail.com                              //
//                                                                                //
// Permission is hereby granted, free of charge, to any person obtaining a copy   //
// of this software and associated documentation files (the "Software"), to deal  //
// in the Software without restriction, including without limitation the rights   //
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell      //
// copies of the Software, and to permit persons to whom the Software is          //
// furnished to do so, subject to the following conditions:                       //
//                                                                                //
// The above copyright notice and this permission notice shall be included in     //
// all copies or substantial portions of the Software.                            //
//                                                                                //
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR     //
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,       //
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE    //
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER         //
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,  //
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN      //
// THE SOFTWARE.                                                                  //
//                                                                                //
// ////////////////////////////////////////////////////////////////////////////// //

#include "structure_io.h"
#include "potential.h"
#include "integrate.h"

//! load each line from a file ignoring commented parts
//! @param is input stream
//! @param comment comment string literal, everyting after comment will be ignored
//! @param lines output string vector that contains each line in the file 
void load(std::istream & is,std::string comment,std::vector<std::string> & lines) 
{ 
	std::string line;
	lines.clear();
	while(std::getline(is,line))
	{
		line = line.substr(0,line.find(comment));
		line.erase(line.find_last_not_of("\n\t\v\f\r ")+1);
		line.erase(0, line.find_first_not_of("\n\t\v\f\r "));
		lines.push_back(line);
	}
}

int main(int argc, char** argv)
{

	std::stringstream text;

	text << "<initial-position>  " << std::endl;
	text << "1  0.0  0.0  0.0 " << std::endl;
	text << "2  1.0  0.0  0.0 " << std::endl;
	text << "3  0.0  1.0  0.0 " << std::endl;
	text << "4  0.0  0.0  1.0 " << std::endl;
	text << "<\\initial-position>  " << std::endl;
	text << "<label>  " << std::endl;
	text << "1:4  H " << std::endl;
	text << "<\\label>  " << std::endl;
	text << "<initial-velocity>  " << std::endl;
	text << "1  0.0  0.0  0.0 " << std::endl;
	text << "2  1.0  0.0  0.0 " << std::endl;
	text << "3  0.0  1.0  0.0 " << std::endl;
	text << "4  0.0  0.0  1.0 " << std::endl;
	text << "<\\initial-velocity>  " << std::endl;
	text << "<mass>  " << std::endl;
	text << "1:4  1.0 " << std::endl;
	text << "<\\mass>  " << std::endl;
	text << "<charge>  " << std::endl;
	text << "1:4  0.0 " << std::endl;
	text << "<\\charge>  " << std::endl;
	text << "<topology>  " << std::endl;
	text << " 2 1 3" << std::endl;
	text << " 4 1" << std::endl;
	text << "<\\topology>  " << std::endl;
	text << "<field>" << std::endl;
	text << "angle harm 4.5  80 :  1,2,3" << std::endl;
	text << "<\\field>" << std::endl;
	std::vector<std::string> input_lines;
	load(text,"#",input_lines);

	/*for(std::vector<std::string>::iterator it = input_lines.begin(); it != input_lines.end(); it++)
		std::cout << *it << std::endl;*/


	structure_t sp;
	input_lines >> in<structure_t>(sp);
	
	std::cout << out<structure_t>(sp);

	velocity_verlet vv(0.0,1.0,0.001,0.1);
	vv(std::cout,sp.atom_list.position,sp.atom_list.velocity,sp);

	//std::cout << out<atom_list_t,def,xyz>(sp.atom_list);
	//std::cout << out<atom_list_t,def,xyz>(sp.atom_list);
	//std::cout << out<atom_list_t,def,xyz>(sp.atom_list);
	//std::cout << out<atom_list_t,def,xyz>(sp.atom_list);

	system("pause");
	return 0;
}


